 <?php

require_once('../../includes/classes/core.php');

//----------------------------- ცვლადი

$agent	= $_REQUEST['agent'];
$queue	= $_REQUEST['queuet'];
$start_time = $_REQUEST['start_time'];
$end_time 	= $_REQUEST['end_time'];
$day = (strtotime($end_time)) -  (strtotime($start_time));
$day_format = ($day / (60*60*24)) + 1;
// ----------------------------------

$data		= array('page' => array(
										'answer_call' => '',
										'technik_info' => '',
										'report_info' => ''
								));


//------------------------------- ტექნიკური ინფორმაცია

	$row_chat = mysql_fetch_assoc(mysql_query("	SELECT  COUNT(*) AS `total`,
                                                        (
                                                        SELECT COUNT(DISTINCT chat.id) AS `count`
                                                        FROM chat
                                                        JOIN chat_detail ON chat.id = chat_detail.chat_id
														LEFT JOIN users ON chat.user_id = users.id
														LEFT JOIN persons ON users.person_id = persons.id
                                                        WHERE DATE(chat.start_date) >= '$start_time'
	                                                    AND DATE(chat.end_date) <= '$end_time'
	                                                    AND chat_detail.type =2
	                                                    AND (TIME(chat.start_date) >= '10:00:00' AND TIME(chat.start_date) <= '22:00:00')
	                                                    AND persons.`name` IN($agent) 
                                                        ) AS `ansswer`,
                                                        (
                                                        SELECT COUNT(*) AS `count`
                                                		FROM `incomming_call`
														LEFT JOIN users ON incomming_call.user_id = users.id
														LEFT JOIN persons ON users.person_id = persons.id
                                                		WHERE DATE(date) >= '$start_time'
                                                	    AND DATE(date) <= '$end_time'
	                                                    AND (TIME(incomming_call.date) >= '10:00:00' AND TIME(incomming_call.date) <= '22:00:00')
                                                	    AND source_id = 2
														AND persons.`name` IN($agent)
                                                        ) AS `done`
                                                FROM chat																								
                                                WHERE DATE(chat.start_date) >= '$start_time'
                                        	    AND DATE(chat.end_date) <= '$end_time'
                                        	    AND (TIME(chat.start_date) >= '10:00:00' AND TIME(chat.start_date) <= '22:00:00')
												"));
	
	
	$data['page']['technik_info'] = '
							
                    <td>ჩატი</td>
                    <td>'.$row_chat[total].'</td>
                    <td>'.$row_chat[ansswer].'</td>
                    <td>'.($row_chat[total] - $row_chat[ansswer]).'</td>
                    <td>'.$row_chat[done].'</td>
                    <td>'.round((($row_chat[ansswer] / $row_chat[total]) * 100),2).' %</td>
                    <td>'.round(((($row_chat[total] - $row_chat[ansswer]) / $row_chat[total]) * 100),2).' %</td>
                    <td>'.round((($row_chat[done] / $row_chat[ansswer]) * 100),2).' %</td>
                
							';
// -----------------------------------------------------
	
//---------------------------------------- რეპორტ ინფო

	$data['page']['report_info'] = '
				
                <tr>
                    <td class="tdstyle">რიგი:</td>
                    <td>'.$queue.'</td>
                </tr>
                <tr>
                    <td class="tdstyle">საწყისი თარიღი:</td>
                    <td>'.$start_time.'</td>
                </tr>
                <tr>
                    <td class="tdstyle">დასრულების თარიღი:</td>
                    <td>'.$end_time.'</td>
                </tr>
                <tr>
                    <td class="tdstyle">პერიოდი:</td>
                    <td>'.$day_format.' დღე</td>
                </tr>

							';
	
//----------------------------------------------

//------------------------------- მომსახურების დონე(Service Level)
	
	
	
	$res_service_level = mysql_query("	
                                	    SELECT 	ROUND(chat_detail.date - chat.start_date) AS `info1`
                                        FROM 	chat
                                        JOIN 	chat_detail ON chat.id = chat_detail.chat_id
                                        LEFT JOIN users ON chat.user_id = users.id
                                        LEFT JOIN persons ON users.person_id = persons.id
                                        WHERE DATE(chat.start_date) >= '$start_time'
                                        AND DATE(chat.end_date) <= '$end_time'
	                                    AND (TIME(chat.start_date) >= '10:00:00' AND TIME(chat.start_date) <= '22:00:00')
                                        AND persons.`name` IN($agent)
                                        AND chat_detail.type = 2
                                        GROUP BY chat.id
                                	 ");
	    $w15 = 0;
	    $w30 = 0;
	    $w45 = 0;
	    $w60 = 0;
	    $w90 = 0;
	    $w120 = 0;
	    $w180 = 0;
	    $w240 = 0;
	    $w300 = 0;	
	
	
	    while ($res_service_level_r = mysql_fetch_assoc($res_service_level)) {
	
	        if ($res_service_level_r['info1'] < 15) {
	            $w15++;
	        }
	
	        if ($res_service_level_r['info1'] < 30){
	            $w30++;
	        }
	
	        if ($res_service_level_r['info1'] < 45){
	            $w45++;
	        }
	
	        if ($res_service_level_r['info1'] < 60){
	            $w60++;
	        }
	
	        if ($res_service_level_r['info1'] < 90){
	            $w90++;
	        }
	
	        if ($res_service_level_r['info1'] < 120){
	            $w120++;
	        }
	        
	        if ($res_service_level_r['info1'] < 180){
	            $w180++;
	        }
	        
	        if ($res_service_level_r['info1'] < 240){
	            $w240++;
	        }
	
	        $w300++;
	
	    }
	
	    $d30 = $w30 - $w15;
	    $d45 = $w45 - $w30;
	    $d60 = $w60 - $w45;
	    $d90 = $w90 - $w60;
	    $d120 = $w120 - $w90;
	    $d180 = $w180 - $w120;
	    $d240 = $w240 - $w180;
	    $d300 = $w300 - $w180;
	
	
	    $p15 = round($w15 * 100 / $w300);
	    $p30 = round($w30 * 100 / $w300);
	    $p45 = round($w45 * 100 / $w300);
	    $p60 = round($w60 * 100 / $w300);
	    $p90 = round($w90 * 100 / $w300);
	    $p120 = round($w120 * 100 / $w300);
	    $p180 = round($w180 * 100 / $w300);
	    $p240 = round($w240 * 100 / $w300);
	
	
	
	
	
	    $data['page']['service_level'] = '
				
							<tr class="odd">
						 		<td>15 წამში</td>
					 			<td>'.$w15.'</td>
					 			<td></td>
					 			<td>'.$p15.'%</td>
					 		</tr>
				 			<tr>
						 		<td>30 წამში</td>
					 			<td>'.$w30.'</td>
					 			<td>'.$d30.'</td>
					 			<td>'.$p30.'%</td>
					 		</tr>
				 			<tr class="odd">
						 		<td>45 წამში</td>
					 			<td>'.$w45.'</td>
					 			<td>'.$d45.'</td>
					 			<td>'.$p45.'%</td>
					 		</tr>
				 			<tr>
						 		<td>60 წამში</td>
					 			<td>'.$w60.'</td>
					 			<td>'.$d60.'</td>
					 			<td>'.$p60.'%</td>
					 		</tr>
				 			<tr class="odd">
						 		<td>90 წამში</td>
					 			<td>'.$w90.'</td>
					 			<td>'.$d90.'</td>
					 			<td>'.$p90.'%</td>
					 		</tr>
					 		<tr>
						 		<td>120 წამში</td>
					 			<td>'.$w120.'</td>
					 			<td>'.$d120.'</td>
					 			<td>'.$p120.'%</td>
					 		</tr>
					 		<tr class="odd">
						 		<td>180 წამში</td>
					 			<td>'.$w180.'</td>
					 			<td>'.$d180.'</td>
					 			<td>'.$p180.'%</td>
					 		</tr>
					 		<tr>
						 		<td>240 წამში</td>
					 			<td>'.$w240.'</td>
					 			<td>'.$d240.'</td>
					 			<td>'.$p240.'%</td>
					 		</tr>
					 		<tr class="odd">
						 		<td>300+ წამში</td>
					 			<td>'.$w300.'</td>
					 			<td>'.$d300.'</td>
					 			<td>100%</td>
					 		</tr>
							';
	
//-------------------------------------------------------


echo json_encode($data);

?>